package com.twu.biblioteca;

        import java.util.ArrayList;
        import java.util.List;

public class MovieRepository {
    List<Movie> moviesInLibrary = new ArrayList<Movie>();
    List<Movie> borrowedMovies = new ArrayList<Movie>();

    public MovieRepository() {
        moviesInLibrary.add(new Movie("Udaan" , "Vikramaditya Motwane", 4.5, 2010));
        moviesInLibrary.add(new Movie("Udaan 2" , "Vikramaditya Motwane", 4, 2012));

    }

    public String movieDisplay(){
        String dispMovies = "";
        for(int i=0;i< moviesInLibrary.size();i++)
            dispMovies+= " Name =" + moviesInLibrary.get(i).movieTitle + " Director =" + moviesInLibrary.get(i).movieDirector + " Rating = " + moviesInLibrary.get(i).movieRating + " Year = " + moviesInLibrary.get(i).movieYear + "\n";
        return dispMovies;
    }

    public boolean checkOutMovie(String name){
        Movie movieToBeCheckedOut = searchMovie(name, moviesInLibrary);
        if(movieToBeCheckedOut!=null){
            borrowedMovies.add(movieToBeCheckedOut);
            moviesInLibrary.remove(movieToBeCheckedOut);
            return true;
        }
        return false;
    }

    public boolean returnMovie(String title){
        Movie returnedMovie = searchMovie(title,borrowedMovies);
        if(borrowedMovies.contains(returnedMovie)){
            borrowedMovies.remove(returnedMovie);

            moviesInLibrary.add(returnedMovie);
            return true;
        }
        return false;
    }

    public Movie searchMovie(String movieTitleToBeSearched, List<Movie> listOfMoviesToSearch){
        for(Movie movie: listOfMoviesToSearch)
            if (movieTitleToBeSearched.equals(movie.movieTitle))
                return movie;
        return null;
    }
}

